# Maintainer: James Ross-Gowan <rossymiles@gmail.com>

_realname=uchardet
pkgbase="mingw-w64-${_realname}-git"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=0.0.1.28.84e292d
pkgrel=1
pkgdesc="An encoding detector library ported from Mozilla (mingw-w64)"
arch=('any')
url='https://github.com/BYVoid/uchardet'
license=('MPL')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("git" "${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-gcc")
source=("${_realname}"::"git+https://github.com/BYVoid/uchardet.git")
sha256sums=('SKIP')

pkgver() {
  cd "${srcdir}/${_realname}"
  local major=$(sed -n 's/^ *set *( *UCHARDET_VERSION_MAJOR *\([0-9][0-9]*\) *).*/\1/p' CMakeLists.txt)
  local minor=$(sed -n 's/^ *set *( *UCHARDET_VERSION_MINOR *\([0-9][0-9]*\) *).*/\1/p' CMakeLists.txt)
  local rev=$(sed -n 's/^ *set *( *UCHARDET_VERSION_REVISION *\([0-9][0-9]*\) *).*/\1/p' CMakeLists.txt)
  printf '%s.%s.%s.%s.%s' $major $minor $rev "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -r "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="$pkgdir" install
}
